from watchdog.observers import Observer
from .file_change_handler import FileChangeHandler


def init_app(app, execute_actions):
    content_dir = app.config['content_dir']
    template_dir = app.config['template_dir']

    # 创建文件变化监听器实例，并传递 execute_actions 方法
    event_handler = FileChangeHandler(execute_actions)

    # 创建 Observer 实例，并将创建好的监听器绑定到 Observer 上
    observer = Observer()
    observer.schedule(event_handler, content_dir, recursive=True)
    observer.schedule(event_handler, template_dir, recursive=True)

    # 启动监听器
    observer.start()

    app.logger.info('File watcher plugin has been initialized.')
    return observer
